package com.easy.order.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.easy.common.bean.OrderItems;
import com.easy.common.bean.Orders;
import com.easy.common.enumbean.OrderStatus;
import com.easy.common.service.IOrdersService;
import com.easy.order.mapper.OrderItemsMapper;
import com.easy.order.mapper.OrdersMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.PrintWriter;
import java.util.List;

@Service
public class OrdersService implements IOrdersService {
    @Autowired
    OrdersMapper ordersMapper;
    @Override
    public Orders insert(Orders orders) {
        ordersMapper.insert(orders);
        Orders result=this.getByID(Integer.parseInt(orders.getId()));
        return result;
    }

    @Override
    public Orders update(Orders orders) {
        ordersMapper.updateById(orders);
        Orders result=this.getByID(Integer.parseInt(orders.getId()));
        return result;
    }

    @Override
    public Orders transact(Integer id) {
        UpdateWrapper wrapper=new UpdateWrapper();
        Orders orders=ordersMapper.selectById(id);
        if(orders.getOrder_status()==OrderStatus.OBLIGATION){
            wrapper.set("order_status",OrderStatus.TRANSACTED);
        }else{

        }
        wrapper.eq("id",id);
        int result=ordersMapper.update(wrapper);
        return orders;
    }

    @Override
    public Orders cancel(Integer id) {
        UpdateWrapper wrapper=new UpdateWrapper();
        Orders orders=ordersMapper.selectById(id);
        if(orders.getOrder_status()==OrderStatus.OBLIGATION){
            wrapper.set("order_status",OrderStatus.CANCELLED);
        }else{

        }
        wrapper.eq("id",id);
        int result=ordersMapper.update(wrapper);
        return orders;
    }


    @Override
    public Orders getByID(Integer id) {
        Orders result=ordersMapper.selectById(id);
        return result;
    }

    @Override
    public IPage<Orders> getListByUserID(IPage page, Integer userid) {
        Wrapper wrapper=new QueryWrapper(getByID(userid));
        IPage result=ordersMapper.selectPage(page,wrapper);
        return result;
    }

    @Override
    public IPage<Orders> getListByCustomerID(IPage page, Integer customerid) {
        Wrapper wrapper=new QueryWrapper(getByID(customerid));
        IPage result=ordersMapper.selectPage(page,wrapper);
        return result;
    }

    @Autowired
    OrderItemsMapper orderitemsMapper;

    @Override
    public OrderItems insertItem(OrderItems items) {
        orderitemsMapper.insert(items);
        return items;
    }

    @Override
    public OrderItems updateItem(OrderItems items) {
        orderitemsMapper.updateById(items);
        return null;
    }

    @Override
    public int deleteItem(Integer id) {
        int num=orderitemsMapper.deleteById(id);
        return num;
    }

}
